package com.anysoft.web;

import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.anysoft.R;
import com.anysoft.base.BaseActivity;
import com.anysoft.event.TimeOutEvent;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by wwq on 2017/4/7.
 */

public class SelectWarmPromptByTypeActivity extends BaseActivity {
    @BindView(R.id.leftButton)
    Button leftButton;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.rightButton)
    Button rightButton;
    @BindView(R.id.titlebar)
    FrameLayout titlebar;
    @BindView(R.id.ll_content)
    LinearLayout llContent;
    private LinearLayout ll_content;

    private String urlStr;
    private String titleStr;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_banner_content);
        ButterKnife.bind(this);

        rightButton.setVisibility(View.GONE);

        // 取出数据
        urlStr = getIntent().getStringExtra("urlStr").toString();

        titleStr = TextUtils.isEmpty(getIntent().getStringExtra("titleStr")) ?
                "温馨提示" : getIntent().getStringExtra("titleStr");

        title.setText(titleStr);

        ll_content = (LinearLayout) findViewById(R.id.ll_content);
        initUrl(urlStr);
    }

    @OnClick(R.id.leftButton)
    public void onClick() {
        finish();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    private void initUrl(String content) {
        content = content.replace("\r\n", "<br>");
        int i = 0;
        while (content.startsWith(" ")) {
            content = content.substring(1, content.length());
            i++;

        }
        for (int j = 0; j < i; j++) {
            content = "&nbsp;" + content;
        }

        TextView text = new TextView(mActivity);
        text.setTextSize(15);
        text.setTextColor(Color.parseColor("#1C1C1C"));

        // 识别内容中的链接
        text.setText(Html.fromHtml(content));
        text.setMovementMethod(LinkMovementMethod.getInstance());
        ll_content.addView(text);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onTimeOutCallBack(TimeOutEvent event) {
        if (event.isSuccess) {
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
